//拓朴排序模板
class Solution {
public:
    vector<vector<int>> g;
    vector<int> bin;
    bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
        int n = numCourses;
        g.resize(n);
        bin.resize(n,0);

        for(int i = 0;i < prerequisites.size();i++)
        {
            int u = prerequisites[i][0];
            int v = prerequisites[i][1];
            g[v].push_back(u);
            bin[u]++;
        }

        queue<int> q;
        for(int i = 0;i < n;i++)
        {
            if(bin[i] == 0) q.push(i);
        }

        int ans = 0;
        while(!q.empty())
        {
            auto cur = q.front();
            q.pop();
            ans++;

            for(int i = 0;i < g[cur].size();i++)
            {
                int next = g[cur][i];
                bin[next]--;

                if(bin[next] == 0)
                {
                    q.push(next);
                }
            }
        }
        return ans == n;
    }
};